﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.IO;

namespace OldArchivedFolderRemover
{
    class Program
    {
        static void Main(string[] args)
        {
            //TODO: get archive folder from web.config
            //TODO: remove folders older than 7 days
            string[] archiveFolders;
            string archiveRoot = ConfigurationManager.AppSettings["archiveRootFolder"];
            archiveFolders = Directory.GetDirectories(archiveRoot);
            foreach (string folder in archiveFolders)
            {
                if(IsOldFolder(folder)){
                    Directory.Delete(folder, true);
                }
            }
        }

        private static bool IsOldFolder(string folder)
        {
            string folderName = folder.Substring(folder.LastIndexOf('/')+1);
            int year = Int32.Parse(folderName.Substring(0,4));
            int month = Int32.Parse(folderName.Substring(4, 2));
            int day = Int32.Parse(folderName.Substring(6, 2));
            int hour =Int32.Parse( folderName.Substring(8, 2));
            int min = Int32.Parse(folderName.Substring(10));
            DateTime folderDT = new DateTime(year, month, day, hour, min, 0);
            DateTime now = DateTime.Now;
            if (folderDT.AddDays(7) < now)
                return true;
            return false;
        }
    }
}
